import pymysql
import json

db = pymysql.connect("192.168.2.251","znet","zheyangkeji","znet" ,cursorclass = pymysql.cursors.DictCursor)

cursor = db.cursor()

cursor.execute("select * from admin_menu")

datas = cursor.fetchall()

result = {
    "child":{},

}

def getC(cqa,code):
    cq = cqa.get("child",None)
    if not cq:
        cq = cqa["child"] = {}
    cc = cq.get(code,None)
    if not cc:
        cc=cq[code] = {}
    return cc


for data in datas:
    menu_number = data["menu_number"]
    wei = len(menu_number)
    count = 0
    cq = result
    for index in range(0,wei,2):
        code = menu_number[int(index):int(index)+2]
        cq = getC(cq,code)
    cq["name"] = data["menu_name"]
    cq["url"] = data["page_url"]
    cq["code"] = menu_number


with open("a.json","w+",encoding="utf8") as f:
    json.dump(result['child'],f,ensure_ascii=False)


